package sjtu.ist.warehouse4ws.tool;

import java.lang.reflect.Field;

public class PropertyHelper {
	/**
	 * 把一个对象中的属性赋值给另一个对象中名字相同的属性(从src复制到dst)
	 * @param src 来源对象
	 * @param dst 目标对象
	 */
	public static void copyProperties(Object src, Object dst){
		Class srcc=src.getClass();
		Field[] srcFields=srcc.getDeclaredFields();
		for(Field field : srcFields){
			Class dstc=dst.getClass();
			Field dstField=null;
			try {
				dstField=dstc.getDeclaredField(field.getName());
			} catch (SecurityException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (NoSuchFieldException e) {
				// TODO Auto-generated catch block
				continue;
			}
			
			Object value=null;
			if(!field.isAccessible()){
				field.setAccessible(true);
			}
			try {
				value=field.get(src);				
			} catch (Exception e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
			
			if(!dstField.isAccessible()){
				dstField.setAccessible(true);
			}
			try {
				dstField.set(dst, value);
			} catch (IllegalArgumentException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			} catch (IllegalAccessException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
	}
	
	public static boolean isStringBlank(String str){
		if(str==null || str.trim().length()==0){
			return true;
		}
		return false;
	}
	
	public static boolean isStringEmpty(String str){
		if(str==null || str.length()==0){
			return true;
		}
		return false;
	}
}
